Skip to content

fix(sqlite): Expand multi-column assignments into individual assignments - #4709

Open
manuelschoene wants to merge 2 commits into
sqlc-dev:mainfrom
manuelschoene:fix/sqlite-multi-column-assignment
Open

manuelschoene wants to merge 2 commits into
sqlc-dev:mainfrom
manuelschoene:fix/sqlite-multi-column-assignment

Conversation

@manuelschoene

@manuelschoene manuelschoene commented Nov 25, 2026 •

Copy link
Copy Markdown

In SQLite, SET (a, b) = (?, ?) (in UPDATE and in an upsert's DO UPDATE SET) names and types every parameter in the row value after the first column of the assignment. Two columns of different types therefore produced wrong Go types (e.g., Name_2 string for an INTEGER column). On the core analyzer path, the parameters came out as Column1 any / Column2 any.

The cause was convertSetPairs in internal/engine/sqlite/convert.go: it converted the right-hand side once and gave the whole row value to every column on the left.

Fixes #4708.

Changes

  • convertSetPairs now gives each column the element of the row value at its position, when the right-hand side is a row value with exactly as many elements as there are columns. (name, score) = (?, ?) becomes name = ?, score = ? in sqlc's AST, so parameter discovery and the core analyzer see ordinary single-column assignments and need no changes.
  • New helper rowElements, which also looks through redundant parentheses (((?, ?))).
  • A right-hand side that is not a row value, such as (a, b) = (SELECT ...), keeps the previous behavior.

I did not reuse ast.MultiAssignRef (as the PostgreSQL engine does) for two reasons: the core analyzer does not handle it, and UpdateStmt.Format merges every target into a single tuple once one of them is a MultiAssignRef, which would print SET (a, b) = (?, ?), c = ? incorrectly.

Tests

New end-to-end case internal/endtoend/testdata/update_set_multiple_columns/sqlite. The columns have different types (TEXT / INTEGER), which the existing update_set_multiple cases do not cover:

  • UPDATE t SET (name, score) = (?, ?) WHERE id = ?
  • the reversed order SET (score, name) = (?, ?)
  • a tuple mixed with a single assignment, SET (name, score) = (?, ?), created = ?
  • INSERT ... ON CONFLICT (id) DO UPDATE SET (name, score) = (?, ?)

The golden file was written before the fix. It is the output of the equivalent single-column assignments (SET name = ?, score = ?), with only the SQL text converted back to tuple form. It failed before the fix and passes after it, with no changes. All four queries are accepted by sqlite3 3.54.1.

  • TestReplay, TestFormat, and TestJsonSchema pass. Database-backed tests were not run locally.
  • Core context (SQLC_TEST_CORE=1): the three UPDATE queries now generate correctly. The case still fails in that context, because the core analyzer ignores ON CONFLICT DO UPDATE and drops the upsert's DO UPDATE parameters entirely. That is a separate issue. The set of failing core cases is identical before and after this change.

CI

I whitelisted four logic errors from the managed-db context. The CI tests the queries of the end-to-end tests against a live SQLite database instance. This instance is not built with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT option enabled. Therefore, the syntax is unknown. As this is expected behavior, I mirrored the whitelist design used in other test cases, such as invalid_params.

Not in this PR

While testing the subquery fallback, I found that UPDATE t SET (name, score) = (SELECT name, score FROM t WHERE id = ?) WHERE id = ? fails on the default path with column reference "id" is ambiguous. It is unrelated to this change: it also happens without it, for single-column assignments, and for plain SELECT ... WHERE x < (SELECT ... FROM t WHERE id = ?) on SQLite and PostgreSQL. It is already reported as #4251 and #3234.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite: Wrong name/type matching inside multi-column assignments

1 participant